package com.jiantong.web.controller.user;

import com.jiantong.user.model.AppUserRole;
import com.jiantong.user.service.AppUserRoleReadService;
import com.jiantong.user.service.AppUserRoleWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by mz on 4/7/17.
 *
 */
@Slf4j
@RestController
public class AppUserRoles {

    @RpcConsumer
    private AppUserRoleReadService appUserRoleReadService;
    @RpcConsumer
    private AppUserRoleWriteService appUserRoleWriteService;

    /**
     * 分页查询app用户角色
     * @param pageNo 页码
     * @param pageSize 每页多少条
     * @param userId 用户id
     * @param status 状态
     * @return Paging<AppUserRole>
     */
    @GetMapping(value = "/api/paging/app/user/roles")
    public Paging<AppUserRole> pagingAppUserRoles(@RequestParam Integer pageNo,
                                                  @RequestParam Integer pageSize,
                                                  @RequestParam Long userId,
                                                  @RequestBody List<Integer> status){
        Response<Paging<AppUserRole>> response = appUserRoleReadService.pagingAppUserRoles(pageNo,pageSize,userId,status);

        if(!response.isSuccess()){
            log.error("failed to paging app user roles case = {}",response.getError());
            throw new OPServerException(response.getError());
        }

        return response.getResult();
    }

    /**
     * 根据主键id查询app用户角色
     * @param id 用户ID
     * @return AppUserRole
     */
    @GetMapping(value = "/api/find/app/user/role/by/id")
    public AppUserRole findAppUserRoleById(@RequestParam Long id){
        if(id == null){
            log.error("id should not be null");
            throw new OPClientException("id should not be null");
        }
        Response<AppUserRole> response = appUserRoleReadService.findAppUserRoleById(id);
        if(!response.isSuccess()){
            log.error("failed to find app user role,id = {}, case = {}",id,response.getError());
            throw new OPServerException(response.getError());
        }

        return response.getResult();
    }


    /**
     * 根据用户id 查询app用户角色
     * @param userId 用户ID
     * @return List<AppUserRole>
     */
    @GetMapping(value = "/api/find/app/user/role/by/user/id")
    public List<AppUserRole> findAppUserRolesByUserId(@RequestParam Long userId){
        if(userId == null){
            log.error("userId should be not null");
            throw new OPClientException("userId should not be null");
        }
        Response<List<AppUserRole>> response = appUserRoleReadService.findAppUserRolesByUserId(userId);
        if(!response.isSuccess()){
            log.error("failed to find app user role,userId = {}, case = {}",userId,response.getError());
            throw new OPServerException(response.getError());
        }

        return response.getResult();
    }

    /**
     * 给指定用户添加一个app用户角色
     * @param appUserRole app用户角色对象
     * @return 主键id
     */
    @PostMapping(value = "/api/add/app/role/for/user")
    public Long addAppRoleForUser(@RequestBody AppUserRole appUserRole){
        Long userId = appUserRole.getUserId();
        if(userId == null){
            log.error("userId should be not null");
            throw new OPClientException("userId should be not null");
        }
        Response<Long> response = appUserRoleWriteService.createAppUserRole(appUserRole);
        if(!response.isSuccess()){
            log.error("failed to add app user role,userId = {}, case = {}",userId,response.getError());
            throw new OPServerException(response.getError());
        }

        return response.getResult();
    }

    /**
     * 给指定用户添加多个app角色
     * @param appUserRoles app用户角色对象list集合
     * @return Boolean
     */
    @PostMapping(value = "/api/add/app/roles/for/user")
    public Boolean addAppRolesForUser(@RequestBody List<AppUserRole> appUserRoles){
        for (AppUserRole appUserRole : appUserRoles){
            Long userId = appUserRole.getUserId();
            if(userId == null){
                log.error("userId should be not null");
                throw new OPClientException("userId should be not null");
            }
        }

        Response<Boolean> response = appUserRoleWriteService.createAppUserRoles(appUserRoles);
        if(!response.isSuccess()){
            log.error("failed to add app user roles,appUserRoles = {}, case = {}",appUserRoles,response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }
}
